package com.greenlife.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenlife.commom.PageInfo;
import com.greenlife.common.enums.BaseResultCodeEnum;
import com.greenlife.common.result.CommonResult;
import com.greenlife.common.result.PageListResult;
import com.greenlife.common.result.SingleResult;
import com.greenlife.controller.util.Paging;
import com.greenlife.converter.ModelConverterVo;
import com.greenlife.model.User;
import com.greenlife.model.po.UserPo;
import com.greenlife.model.vo.UserVo;
import com.greenlife.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;

	/** 1.登陆接口 **/
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult userLogin(@ModelAttribute(" user ") User user, HttpSession session) {
		User values = userService.selectByUser(user);
		SingleResult<UserVo> result = new SingleResult<UserVo>(BaseResultCodeEnum.ERR_CODE_200,
				ModelConverterVo.userToUserVo(values));
		session.setAttribute("user", values.getId());
		logger.debug("登陆成功");
		return result;
	}

	/** 2.注册接口 **/
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult userRegister(@ModelAttribute(" user ") User user) {
		User values = userService.insertSelective(user);
		SingleResult<UserVo> result = new SingleResult<UserVo>(BaseResultCodeEnum.ERR_CODE_200,
				ModelConverterVo.userToUserVo(values));
		logger.debug("注册成功");
		return result;
	}

	/** 3.获取用户列表 **/
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getUsers(User user, PageInfo pageInfo, String orderBy) {
		pageInfo.setOffset((pageInfo.getCurPage() - 1) * pageInfo.getItemsPerPage());
		Sort sort = Paging.getPageSort(orderBy);
		pageInfo.setSort(sort);
		List<User> users = userService.selectUserByPage(user, pageInfo);
		PageListResult<User> result = new PageListResult<User>(BaseResultCodeEnum.ERR_CODE_200, users, pageInfo);
		logger.debug("查看用户列表成功");
		return result;
	}

	/** 4.删除用户接口 **/
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult userDelete(@ModelAttribute(" user ") User user) {
		userService.deleteByPrimaryKey(user);
		CommonResult result = new CommonResult(BaseResultCodeEnum.ERR_CODE_200);
		logger.debug("删除用户成功");
		return result;
	}

	/** 5.查询用户详情接口 **/
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult userSelect(@ModelAttribute(" user ") User user) {
		User values = userService.selectByPrimaryKey(user);
		SingleResult<User> result = new SingleResult<User>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.debug("查询成功");
		return result;
	}

	/** 6.更新用户信息接口 **/
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult userUpdate(@ModelAttribute(" user ") User user, HttpSession session) {
		// user.setId((int)session.getAttribute("userId"));
		userService.updateByPrimaryKeySelective(user);
		CommonResult result = new CommonResult(BaseResultCodeEnum.ERR_CODE_200);
		logger.debug("更新用户信息成功");
		return result;
	}

	/** 7.修改用户密码接口 **/
	@RequestMapping(value = "/psw", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult userUpdatePassword(@ModelAttribute(" userPo ") UserPo userPo) {
		userService.updateUserPassword(userPo);
		CommonResult result = new CommonResult(BaseResultCodeEnum.ERR_CODE_200);
		logger.debug("更新用户信息成功");
		return result;
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult userUpdatePassword(HttpSession session) {
		try {
			System.out.println((int) session.getAttribute("user"));
			SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200,
					(int) session.getAttribute("user"));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		SingleResult<String> result = new SingleResult<String>(BaseResultCodeEnum.ERR_CODE_200, "session没有数据");
		return result;
	}

}
